using System;
using System.Collections.Generic;
using System.Text;

using HomeLite.View;
using HomeLite.LoginWebService;

namespace HomeLite.Controllers
{
    public class UserController
    {
        #region Fields
        private LoginWS _loginWS;
        private string _password;
        private string _username;
        private Guid _userGuid;
        #endregion

        #region Singleton
        private static UserController _instance;            
        public static UserController Instance
        {
            get
            {
                if (_instance == null) _instance = new UserController();
                return _instance;
            }
        }
        #endregion

        #region Properties
        public string Username
        {
            get { return _username; }
            set { _username = value; }
        }
        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }
        #endregion

        #region Constructor | Dispose
        public UserController()
        {
            // Inicializamos el servicio web
            _loginWS = new LoginWS();
            _loginWS.Credentials = System.Net.CredentialCache.DefaultCredentials;
        }
        internal void Dispose()
        {
        } 
        #endregion

        #region Event Handlers
        public event EventHandler Authenticated;
        public event EventHandler Exit;
        #endregion

        #region Methods
        internal void AuthenticateUser()
        {
            FormLogin formLogin;
            Settings settings = Settings.Default;
            String[] lastLogins = settings.HomeLite_LastLogins;
            formLogin = new FormLogin(lastLogins);
            formLogin.LoginClick += new LoginBtnHandler(formLogin_LoginClick);
            formLogin.ExitEvent += new EventHandler(formLogin_ExitEvent);
            formLogin.Show();
        }
        #endregion

        #region Events
        void formLogin_LoginClick(object sender, LoginEventArgs e)
        {
            FormLogin formLogin = (FormLogin)sender;
            Settings settings = Settings.Default;
            List<string> lastLogins = new List<string>();
            bool response;

            if (settings.HomeLite_LastLogins != null)
                lastLogins.AddRange(settings.HomeLite_LastLogins);

            response = _loginWS.AuthenticateUserGUID(e.Email, e.Password, out this._userGuid);

            if (response)
            {
                formLogin.Dispose();

                if (!lastLogins.Contains(e.Email))
                {
                    lastLogins.Add(e.Email);
                    settings.HomeLite_LastLogins = lastLogins.ToArray();
                    settings.Save();
                }
                this._username = e.Email;
                this._password = e.Password;
                this.Authenticated(this, new EventArgs());
            }
            else
            {
                formLogin.SetMessage("Error de acceso");
            }
        }

        void formLogin_ExitEvent(object sender, EventArgs e)
        {
            this.Exit(sender, e);
        }
        #endregion

    }
}
